package com.macro.mall.portal.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.github.pagehelper.PageHelper;
import com.macro.mall.dto.*;
import com.macro.mall.mapper.BmsMuseumApplicantMapper;
import com.macro.mall.mapper.BmsMuseumGroupMapper;
import com.macro.mall.mapper.BmsMuseumTeamMapper;
import com.macro.mall.model.*;
import com.macro.mall.portal.service.BmsMuseumApplicantService;
import org.apache.commons.compress.utils.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class BmsMuseumApplicantServiceImpl extends ActivitiesBaseServiceImpl implements BmsMuseumApplicantService {

    @Autowired
    private BmsMuseumApplicantMapper bmsMuseumApplicantMapper;

    @Autowired
    private BmsMuseumGroupMapper bmsMuseumGroupMapper;

    @Autowired
    private BmsMuseumTeamMapper bmsMuseumTeamMapper;

    @Override
    public List<BmsMuseumApplicant> getList(BmsMuseumApplicantParam bmsMuseumApplicantParam) {
        if (Objects.isNull(bmsMuseumApplicantParam.getMuseumGroupId())) {
            return Lists.newArrayList();
        }
        Integer pageNum = bmsMuseumApplicantParam.getPageNum();
        Integer pageSize = bmsMuseumApplicantParam.getPageSize();
        if (Objects.nonNull(pageNum) && Objects.nonNull(pageSize)) {
            PageHelper.startPage(pageNum, pageSize);
        }
        if (Objects.isNull(bmsMuseumApplicantParam.getApplicationType())) {
            throw new NullPointerException("applicationType is null");
        }

        BmsMuseumApplicantExample example = new BmsMuseumApplicantExample();
        BmsMuseumApplicantExample.Criteria criteria = example.createCriteria();
        criteria.andIsDeleteEqualTo(String.valueOf(0));
        criteria.andMuseumGroupIdEqualTo(bmsMuseumApplicantParam.getMuseumGroupId());
        criteria.andApplicationTypeEqualTo(bmsMuseumApplicantParam.getApplicationType());
        if (!bmsMuseumApplicantParam.getApplicationType().equals("01")){
            if (Objects.isNull(bmsMuseumApplicantParam.getMuseumTeamId())) {
                throw new NullPointerException("museumTeamId is null");
            }
            criteria.andMuseumGroupIdEqualTo(bmsMuseumApplicantParam.getMuseumTeamId());
        }
        if (StringUtils.isNotBlank(bmsMuseumApplicantParam.getUserName())) {
            criteria.andUserNameLike("%" + bmsMuseumApplicantParam.getUserName() + "%");
        }
        if (StringUtils.isNotBlank(bmsMuseumApplicantParam.getUserPhone())) {
            criteria.andUserPhoneLike("%" + bmsMuseumApplicantParam.getUserPhone() + "%");
        }
        if (Objects.nonNull(bmsMuseumApplicantParam.getUserIdType())) {
            criteria.andUserIdTypeEqualTo(bmsMuseumApplicantParam.getUserIdType());
        }
        if (StringUtils.isNotBlank(bmsMuseumApplicantParam.getUserIdCard())) {
            criteria.andUserIdCardLike("%" + bmsMuseumApplicantParam.getUserIdCard() + "%");
        }
        if (Objects.nonNull(bmsMuseumApplicantParam.getApplicationTime())) {
            criteria.andApplicationTimeGreaterThanOrEqualTo(bmsMuseumApplicantParam.getApplicationTime());
        }
        if (StringUtils.isNotBlank(bmsMuseumApplicantParam.getStatus())) {
            criteria.andStatusEqualTo(bmsMuseumApplicantParam.getStatus());
        }
        List<BmsMuseumApplicant> list = bmsMuseumApplicantMapper.selectByExample(example);
        return list;
    }

    @Override
    public BmsMuseumApplicantDetail getItem(Long id) {
        BmsMuseumApplicant bmsMuseumApplicant = bmsMuseumApplicantMapper.selectByPrimaryKey(id);
        BmsMuseumApplicantDetail detail = new BmsMuseumApplicantDetail();
        BeanUtils.copyProperties(bmsMuseumApplicant, detail);
        return detail;
    }

    @Override
    public List<BmsMuseumApplicant> findGroupApplicantByMuseumId(BmsMuseumGroupParam bmsMuseumGroupParam) {
        return bmsMuseumApplicantMapper.findGroupApplicantByMuseumId(bmsMuseumGroupParam);
    }

    @Override
    Boolean checkBaseInfo(ActivitiesRegistrationParam registrationParam) {
        List<ApplicantDetail> applicantUserList = registrationParam.getApplicantUserList();
        BmsMuseumApplicantExample example = new BmsMuseumApplicantExample();
        example.createCriteria().andIsDeleteEqualTo(String.valueOf(0)).andMuseumGroupIdEqualTo(Long.valueOf(registrationParam.getActivitiesId()))
                .andUserIdCardIn(applicantUserList.parallelStream().map(ApplicantDetail::getIdCardNo).collect(Collectors.toList()))
                .andStatusEqualTo("0");
        List<BmsMuseumApplicant> bmsMuseumApplicants = bmsMuseumApplicantMapper.selectByExample(example);
        return CollectionUtil.isEmpty(bmsMuseumApplicants);
    }

    @Override
    int getActivitiesRegistratyCount(ActivitiesRegistrationParam registrationParam) {
        return registrationParam.getApplicantUserList().size();
    }

    @Override
    int getActivitiesHasRegistratyCount(ActivitiesRegistrationParam registrationParam) {
        String activitiesId = registrationParam.getActivitiesId();
        UmsMember wxUser = registrationParam.getWxUser();
        LocalDateTime applicationTime = registrationParam.getApplicationTime();
        String applicationType = registrationParam.getApplicationType();
        BmsMuseumApplicantExample example = new BmsMuseumApplicantExample();
        example.createCriteria().andIsDeleteEqualTo("0").andStatusEqualTo("0")
                .andMuseumGroupIdEqualTo(Long.valueOf(activitiesId))
                .andUserIdEqualTo(wxUser.getId())
                .andApplicationTypeEqualTo(applicationType);
        List<BmsMuseumApplicant> bmsMuseumApplicantList = bmsMuseumApplicantMapper.selectByExample(example);
        long count = bmsMuseumApplicantList.parallelStream().filter(i -> i.getApplicationTime().toLocalDate().equals(applicationTime.toLocalDate())).count();
        return Math.toIntExact(count);
    }

    @Override
    ActivitiesRegistrationDetail getActivitiesDetail(String activitiesId) {
        BmsMuseumGroup bmsMuseumGroup = bmsMuseumGroupMapper.getById(Long.valueOf(activitiesId));
        ActivitiesRegistrationDetail detail = new ActivitiesRegistrationDetail();
        detail.setCost(Objects.isNull(bmsMuseumGroup.getCost())? BigDecimal.ZERO : bmsMuseumGroup.getCost());
        detail.setApplicantCount(bmsMuseumGroup.getPeopleNumber());
        return detail;
    }

    @Override
    int insetApplicantList(ActivitiesRegistrationParam registrationParam) {
        List<ApplicantDetail> applicantUserList = registrationParam.getApplicantUserList();
        UmsMember wxUser = registrationParam.getWxUser();
        LocalDateTime applicationTime = registrationParam.getApplicationTime();
        String applicationType = registrationParam.getApplicationType();
        String activitiesId = registrationParam.getActivitiesId();
        Long teamId = null;
        if (applicationType.equals("1")){
            BmsMuseumTeam bmsMuseumTeam = new BmsMuseumTeam();
            bmsMuseumTeam.setApplicationTime(applicationTime);
            bmsMuseumTeam.setMuseumGroupId(Long.valueOf(activitiesId));
            bmsMuseumTeam.setName(registrationParam.getTeamName());
            bmsMuseumTeam.setLeader(registrationParam.getTeamLeader());
            bmsMuseumTeam.setPhone(registrationParam.getTeamPhone());
            int i = bmsMuseumTeamMapper.insertSelective(bmsMuseumTeam);
            if (i == 0) {
                return 0;
            }
            teamId = bmsMuseumTeam.getId();
        }

        BmsMuseumApplicant bmsMuseumApplicant;
        int count = 0;
        for (ApplicantDetail applicantDetail : applicantUserList) {
            bmsMuseumApplicant = new BmsMuseumApplicant();
            bmsMuseumApplicant.setApplicationType(applicationType);
            bmsMuseumApplicant.setApplicationTime(applicationTime);
            bmsMuseumApplicant.setMuseumGroupId(Long.valueOf(activitiesId));
            bmsMuseumApplicant.setShouldPayment(registrationParam.getShouldPayment());
            bmsMuseumApplicant.setActualPayment(registrationParam.getActualPayment());
            bmsMuseumApplicant.setUserId(wxUser.getId());
            bmsMuseumApplicant.setUserPhone(applicantDetail.getPhone());
            bmsMuseumApplicant.setUserIdType(Long.valueOf(applicantDetail.getIdCardType()));
            bmsMuseumApplicant.setUserIdCard(applicantDetail.getIdCardNo());
            bmsMuseumApplicant.setUserName(applicantDetail.getName());
            bmsMuseumApplicant.setStatus("0");
            bmsMuseumApplicant.setIsDelete(String.valueOf(0));
            bmsMuseumApplicant.setCreateUser(wxUser.getId());
            bmsMuseumApplicant.setCreateTime(new Date());
            bmsMuseumApplicant.setUpdateUser(wxUser.getId());
            bmsMuseumApplicant.setUpdateTime(new Date());
            bmsMuseumApplicant.setMuseumTeamId(teamId);
            int i = bmsMuseumApplicantMapper.insertSelective(bmsMuseumApplicant);
            count += i;
        }
        return count;
    }

    @Override
    int handleApplicantStatus(ActivitiesRegistrationParam registrationParam, String status) {
        String activitiesId = registrationParam.getActivitiesId();
        UmsMember wxUser = registrationParam.getWxUser();
        LocalDateTime applicationTime = registrationParam.getApplicationTime();
        String applicationType = registrationParam.getApplicationType();
        BmsMuseumApplicantExample example = new BmsMuseumApplicantExample();
        example.createCriteria().andIsDeleteEqualTo("0").andStatusEqualTo("0")
                .andMuseumGroupIdEqualTo(Long.valueOf(activitiesId))
                .andUserIdEqualTo(wxUser.getId())
                .andApplicationTypeEqualTo(applicationType);
        List<BmsMuseumApplicant> bmsMuseumApplicantList = bmsMuseumApplicantMapper.selectByExample(example);
        List<Long> idList = bmsMuseumApplicantList.parallelStream().filter(i -> i.getApplicationTime().toLocalDate().equals(applicationTime.toLocalDate())).map(BmsMuseumApplicant::getId).collect(Collectors.toList());

        BmsMuseumApplicantExample updateExample = new BmsMuseumApplicantExample();
        updateExample.createCriteria().andIdIn(idList);

        BmsMuseumApplicant bmsMuseumApplicant = new BmsMuseumApplicant();
        bmsMuseumApplicant.setStatus(status);

        return bmsMuseumApplicantMapper.updateByExampleSelective(bmsMuseumApplicant, updateExample);
    }
}
